package com.lin.alipay.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @作者: 林江
 * @创建时间: 2023/11/15
 * @功能:
 */
@Controller
public class AlipayPhoneController {
    final static String GATEWAY = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    final static String APP_ID = "9021000122673771";
    final static String CHARSET = "UTF-8";
    final static String PRIVETE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCfarvjmxGiWVb9mzJL96f8rsOuR7vUGKYWzyhaMr2zkFHGTbyJ2dhRu+hItjcoCYVTmJ7SGoPX7dAuLXuhT0Z8b4UhSh7m1GEvUy1uWJrmjblRMm+3EqS6JUABmpzMVvtUmaHjqzPQzlUSlAN4Pk120+15kv6EBlIGpSnVfTdvvyDkPzuD8KaDaAI1BAZrk2Y/FqEPMaUbJIGxhCV4/d72/JCXE3U93/bHA+qup26obbZJw0grtxiF2xbYuK0293YMMb0/C3pDZu7gm7Dma+XWM5lj4KAfBis2rGCNgGSJMBfgqL6BMypycLK4p9FmnzdR8SzSfdyLxTi4ocmPkqovAgMBAAECggEBAJDBpLc6ujqHkXxcDsH4smu6bim1LZOchkSz+1pcNPH2nKrLL+alD864KGbOPyt16cFNFvqF5S6P3bfwyRBjIgggY+0klwbX1x/YEarffljYXGyktdixby8wX/yU3siWO0ZI8z3+2eBDW/Cwwq3PNRwvuLXqin5sFtO96kzXlR08K16Py7CGZ8mzsLOC5BGlOHnjGTCkADyc4RxvD8jYp2FUKGjmc/HlvEhnf7eXcPtK4mcCuVByTPvbMeyzrXrtEMAQVDzYy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Bcqpmf0XUlU2DP8SMmXh6bD3IE3xaFdreSHfmPbz/m38ApRi/sNcHPPAXag1kXhygr2fzZZapsVUjxw5pRlScpTE/CRST0uR3ziRlIE/W/s8rMMrSu5qqEP9Y64TyC5XATXLSGC4uJ7kTcpovZuTvEUGr11jo0vGZNQpAR6HNSUxK93wqQ7Kp1dcXs/UFGTPHWMj0d1g1FbJzO6id1eHoWgl6DmgHDT90Z2gWxkE7YQoTrltMh/LuitgkNNJBfRwr3X1nztkNwgjoD/2FZkzBxvZQCgwIDAQAB";

    @RequestMapping(value = "/createPayOrder", method = {RequestMethod.GET, RequestMethod.POST})
    public void createPayOrder(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        // 创建alipay客户端
        AlipayClient alipayClient = new DefaultAlipayClient(
                GATEWAY,
                APP_ID,
                PRIVETE_KEY,
                "json",
                CHARSET,
                PUBLIC_KEY,
                "RSA2"
        );

        // 构造支付宝请求参数
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        //异步回调地址（开发者业务接口），仅支持http/https，公网可访问
        request.setNotifyUrl("");
        //同步跳转地址，仅支持http/https
        request.setReturnUrl("");

        /******必传参数******/
        JSONObject bizContent = new JSONObject();
        //商户订单号，商家自定义，保持唯一性
        bizContent.put("out_trade_no", "20210817010101008");
        //支付金额，最小值0.01元
        bizContent.put("total_amount", 0.01);
        //订单标题，不可使用特殊符号
        bizContent.put("subject", "测试商品");

        /******可选参数******/
        //手机网站支付默认传值FAST_INSTANT_TRADE_PAY
        bizContent.put("product_code", "QUICK_WAP_WAY");
        //bizContent.put("time_expire", "2022-08-01 22:00:00");

        //// 商品明细信息，按需传入
        //JSONArray goodsDetail = new JSONArray();
        //JSONObject goods1 = new JSONObject();
        //goods1.put("goods_id", "goodsNo1");
        //goods1.put("goods_name", "子商品1");
        //goods1.put("quantity", 1);
        //goods1.put("price", 0.01);
        //goodsDetail.add(goods1);
        //bizContent.put("goods_detail", goodsDetail);

        //// 扩展信息，按需传入
        //JSONObject extendParams = new JSONObject();
        //extendParams.put("sys_service_provider_id", "2088511833207846");
        //bizContent.put("extend_params", extendParams);

        request.setBizContent(bizContent.toString());
        // 发送请求
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
        if (response.isSuccess()) {
            String body = response.getBody();
            // 设置响应头
            resp.setContentType("text/html;charset=" + CHARSET);
            PrintWriter writer = resp.getWriter();
            writer.write(body);
            writer.flush();
            writer.close();
        } else {
        }
    }
}
